Object.defineProperty(exports, "__esModule", {
    value: !0
});

var t, e = require("./obaa"), a = (t = e) && t.__esModule ? t : {
    default: t
}, s = require("./path");

function i(t, e) {
    var a, i, r, c, h, u;
    2 === arguments.length ? (t.instances || (t.instances = {}), t.__changes_ || (t.__changes_ = []), 
    a = t.__changes_, t.onChange || (t.onChange = function(t) {
        a.push(t);
    }), t.offChange || (t.offChange = function(t) {
        for (var e = 0, s = a.length; e < s; e++) if (a[e] === t) {
            a.splice(e, 1);
            break;
        }
    }), i = void 0 !== e.data, r = void 0, e.data ? (r = JSON.parse(JSON.stringify(e.data)), 
    e.data.$ = t.data) : e.data = t.data, o(t), c = e.onLoad, h = e.onUnload, e.onLoad = function(a) {
        this.store = t, e.use && (this.__updatePath = (0, s.getPath)(e.use)), this.__use = e.use, 
        (this.__hasData = i) && Object.assign(e.data, JSON.parse(JSON.stringify(r))), t.instances[this.route] = t.instances[this.route] || [], 
        t.instances[this.route].push(this), this.computed = e.computed, this.setData(e.data);
        var o = (0, s.getUsing)(t.data, e.use);
        e.computed && n(e.computed, t, o, this), this.setData(o), c && c.call(this, a);
    }, e.onUnload = function(e) {
        var a = this;
        t.instances[this.route] = t.instances[this.route].filter(function(t) {
            return t !== a;
        }), h && h.call(this, e);
    }, Page(e)) : (t.lifetimes = t.lifetimes || {}, u = t.lifetimes.ready || t.ready, 
    t.ready = t.lifetimes.ready = function() {
        var e = getCurrentPages()[getCurrentPages().length - 1];
        t.use && (this.__updatePath = (0, s.getPath)(t.use)), this.store = e.store, this.__use = t.use, 
        this.computed = t.computed, t.data = this.store.data, this.setData(t.data);
        var a = (0, s.getUsing)(this.store.data, t.use);
        t.computed && n(t.computed, this.store, a, this), this.setData(a), e._omixComponents = e._omixComponents || [], 
        e._omixComponents.push(this), u && u.call(this);
    }, Component(t));
}

function n(t, e, a, s) {
    for (var i in t) a[i] = t[i].call(e.data, s);
}

function o(t) {
    var e = (0, a.default)(t.data, function(e, a, i, n) {
        var o = {};
        if (0 === e.indexOf("Array-push")) for (var c = a.length - i.length, h = 0; h < c; h++) o[(0, 
        s.fixPath)(n + "-" + (i.length + h))] = a[i.length + h]; else 0 === e.indexOf("Array-") ? o[(0, 
        s.fixPath)(n)] = a : o[(0, s.fixPath)(n + "-" + e)] = a;
        !function(t, e) {
            for (var a in e.instances) e.instances[a].forEach(function(a) {
                r(t, e, a), a._omixComponents && a._omixComponents.forEach(function(a) {
                    r(t, e, a);
                });
            });
            e.__changes_.forEach(function(e) {
                e(t);
            }), e.debug && function(t, e) {
                try {
                    var a = wx.getStorageSync("CurrentState") || {};
                    console.groupCollapsed("%c  Data Changed %c " + Object.keys(e), "color:#e0c184; font-weight: bold", "color:#f0a139; font-weight: bold"), 
                    console.log("%c    Pre Data", "color:#ff65af; font-weight: bold", a), console.log("%c Change Data", "color:#3d91cf; font-weight: bold", e), 
                    console.log("%c   Next Data", "color:#2c9f67; font-weight: bold", t.data), console.groupEnd(), 
                    wx.setStorageSync("CurrentState", t.data);
                } catch (t) {
                    t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t);
                    console.log(t);
                }
            }(e, t);
        }(o, t);
    });
    t.set || (t.set = function(t, s, i) {
        a.default.set(t, s, i, e);
    });
    var i = t.data;
    Object.defineProperty(t, "data", {
        enumerable: !0,
        get: function() {
            return i;
        },
        set: function() {
            throw new Error("You must not replace store.data directly, instead assign nest prop");
        }
    });
}

function r(t, e, a) {
    if (e.updateAll || a.__updatePath && (0, s.needUpdate)(t, a.__updatePath)) {
        if (!a.__hasData) return c(t, e, a);
        var i, n = Object.assign({}, t);
        for (i in n) /\$\./.test(i) || (n["$." + i] = t[i], delete n[i]);
        c(n, e, a);
    }
}

function c(t, e, a) {
    if (!wx.nextTick) return h(t, e, a);
    void 0 === a._omixDataBuffer && (a._omixDataBuffer = {}), Object.assign(a._omixDataBuffer, t), 
    a._omixTickScheduled || (wx.nextTick(function() {
        h(a._omixDataBuffer, e, a), a._omixDataBuffer = {}, a._omixTickScheduled = !1;
    }), a._omixTickScheduled = !0);
}

function h(t, e, a) {
    0 !== Object.keys(t).length && (a.setData.call(a, t), t = (0, s.getUsing)(e.data, a.__use), 
    a.computed && n(a.computed, e, t, a), a.setData.call(a, t));
}

i.Page = function(t, e) {
    i(t, e);
}, i.Component = function(t, e) {
    var a, i, r, c, h, u, d;
    2 === arguments.length ? (t.instances || (t.instances = {}), t.__changes_ || (t.__changes_ = []), 
    a = t.__changes_, t.onChange || (t.onChange = function(t) {
        a.push(t);
    }), t.offChange || (t.offChange = function(t) {
        for (var e = 0, s = a.length; e < s; e++) if (a[e] === t) {
            a.splice(e, 1);
            break;
        }
    }), i = void 0 !== e.data, r = void 0, e.data ? (r = JSON.parse(JSON.stringify(e.data)), 
    e.data.$ = t.data) : e.data = t.data, o(t), c = e.detached, e.lifetimes = e.lifetimes || {}, 
    h = e.lifetimes.created || e.created, u = e.lifetimes.ready || e.ready, e.created = e.lifetimes.created = function(a) {
        this.store = t, e.use && (this.__updatePath = (0, s.getPath)(e.use)), this.__use = e.use, 
        (this.__hasData = i) && Object.assign(e.data, JSON.parse(JSON.stringify(r))), h && h.call(this, a);
    }, e.ready = e.lifetimes.ready = function(t) {
        var a = this.store;
        a.instances[this.is] = a.instances[this.is] || [], a.instances[this.is].push(this), 
        this.computed = e.computed, this.setData(e.data);
        var i = (0, s.getUsing)(a.data, e.use);
        e.computed && n(e.computed, a, i, this), this.setData(i), u && u.call(this, t);
    }, e.lifetimes.detached = e.detached = function(t) {
        var e = this;
        this.store.instances[this.is] = (this.store.instances[this.is] || []).filter(function(t) {
            return t !== e;
        }), c && c.call(this, t);
    }, Component(e)) : (t.lifetimes = t.lifetimes || {}, d = t.lifetimes.ready || t.ready, 
    t.ready = t.lifetimes.ready = function() {
        var e = getCurrentPages()[getCurrentPages().length - 1];
        t.use && (this.__updatePath = (0, s.getPath)(t.use)), this.store = e.store, this.__use = t.use, 
        this.computed = t.computed, t.data = this.store.data, this.setData(t.data);
        var a = (0, s.getUsing)(this.store.data, t.use);
        t.computed && n(t.computed, this.store, a, this), this.setData(a), e._omixComponents = e._omixComponents || [], 
        e._omixComponents.push(this), d && d.call(this);
    }, Component(t));
}, i.obaa = a.default, exports.default = i;